﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HWmanagementDB;

namespace HWmanagementServer
{
    public class SubjectMgr
    {
        //getting the courses for a subject
        public static List<CourseModel> getCourses(string subject)
        {
            try
            {
                List<CourseModel> ret = new List<CourseModel>();
                using (Repository repo = new Repository())
                {

                    IEnumerator<COURSE> courses = repo.CourseRepository.Get(filter: (c) => c.SUBJECTID == subject).GetEnumerator();
                    while (courses.MoveNext())
                    {
                        ret.Add(CourseModel.fromDBTable(courses.Current));
                    }
                }
                return ret;
            }
            catch
            {
                throw new Exception("Error while handling request!");
            }
        }

        //getting the list of all subjects
        public static List<SubjectModel> getAllSubjects()
        {
            try
            {
                List<SubjectModel> ret = new List<SubjectModel>();
                using (Repository repo = new Repository())
                {
                    IEnumerator<SUBJECT> subs = repo.SubjectRepository.Get().GetEnumerator();
                    while (subs.MoveNext())
                    {
                        ret.Add(SubjectModel.fromDBTable(subs.Current));
                    }
                }
                return ret;
            }
            catch
            {
                throw new Exception("Error while handling request!");
            }
        }

        //getting the list of teachers by subject id
        public static List<UserModel> getTeachers(string subjectId)
        {
            try
            {
                List<UserModel> ret = new List<UserModel>();
                using (Repository repo = new Repository())
                {                    
                    SUBJECT sub = repo.SubjectRepository.GetByID(subjectId);

                    List<USER> tea = (from u in sub.USER
                                      where u.RANK == 1
                                      select u).ToList();

                    for (int i = 0; i < tea.Count; i++)
                    {
                        ret.Add(UserModel.fromDBTable(tea.ElementAt(i)));
                    }

                }
                return ret;
            }
            catch
            {
                 throw new Exception("Error while handling request!");
            }
        }

    }
}
